﻿using ARC.AliYun.SMSService.Models;
using ARC.AliYun.SMSService.Models.DigitalSmsModels;
using ARC.AliYun.SMSService.Models.SimpleSmsModels;
using ARC.API.ExternalInterface.CurrencyInterface;
using ARC.API.ExternalInterface.Models;
using ARC.API.ExternalInterface.Models.AddSign;
using ARC.API.ExternalInterface.Models.AddTemplate;
using ARC.API.ExternalInterface.Models.QuerySign;
using ARC.API.ExternalInterface.Models.SendDetails;
using ARC.API.ExternalInterface.Models.SendSms;
using ARC.Yunpian.SMSService.Models.SimpleSmsModels;
using Newtonsoft.Json;
using System.Collections.Generic;
using ARC.YunXinShi.SMSService.Models.SimpleSmsModels;
using QuerySimpleSmsSinResponse = ARC.AliYun.SMSService.Models.SimpleSmsModels.QuerySimpleSmsSinResponse;

namespace ARC.API.ExternalInterface.CurrencyService
{
    public class CurrencyInterfaceSdkSmsClient : ICurrencyInterfaceSdkSmsClient
    {
        #region 发送普通短信
        public SendResponse SendAliYunSimpleSms(string accessKeyId, string secret, string phoneNumber, string signName,
          string templateCode, string templateParam = "")
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(signName) || string.IsNullOrEmpty(templateCode))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            ARC.AliYun.SMSService.Models.SendSmsResponse response = GetAliYunSimpleInstans(accessKeyId, secret).SendSimpleSms(phoneNumber, signName, templateCode, templateParam);
            if (response.Code != "OK")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new SendResponse() { Code = Status.Success, BilzId = response.BizId, Msg = response.Message };
        }

        public SendResponse SendAliYunSimpleBatch(string accessKeyId, string secret, List<string> phoneNumbers, List<string> signNameJson,
            string templateCode, List<Dictionary<string, string>> templateParam = null)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateCode))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            if (phoneNumbers == null || signNameJson == null) return new SendResponse() { Code = Status.ParamenterIsNull, Msg = "必填参数不可为空" };
            if (phoneNumbers.Count == 0 || signNameJson.Count == 0) return new SendResponse() { Code = Status.ParamenterIsNull, Msg = "必填参数不可为空" };
            ARC.AliYun.SMSService.Models.SendSmsResponse response = GetAliYunSimpleInstans(accessKeyId, secret)
                .SendSimpleSmsBatch(phoneNumbers, signNameJson, templateCode, templateParam);
            if (response.Code != "OK")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new SendResponse() { Code = Status.Success, BilzId = response.BizId, Msg = response.Message };
        }

        public SendResponse SendYunPianSimpleSms(string apikey, string mobile, string tpl_id = "", string tpl_value = "", string callback_url = "",
            string extend = "", string uid = "")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(mobile))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            SendSimpleResponse response = GetYunPianSimpleInstans(apikey).JustTelempateSendSms(mobile, tpl_id, tpl_value, callback_url, extend, uid);
            if (response.code != 0)
            {
                return new SendResponse() { Code = Status.Error, Msg = response.msg };
            }
            return new SendResponse() { Code = Status.Success, Msg = response.msg };
        }

        public SendResponse SendSearchYunPianSimpleSms(string apikey, string mobile, string text, string extend = "",
            string uid = "", string callback_url = "", bool register = false,
            bool mobile_stat = true)
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(text))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            SendSimpleResponse response = GetYunPianSimpleInstans(apikey).SendSmsOne(mobile, text, mobile, callback_url, extend);
            if (response.code != 0)
            {
                return new SendResponse() { Code = Status.Error, Msg = response.msg };
            }
            return new SendResponse() { Code = Status.Success, Msg = response.msg };
        }
        public SendResponse SendYunPianSimpleBatch(string apikey, string mobile, string tpl_id, string tpl_value = "")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(mobile))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            SendSimpleBatchResponse response = GetYunPianSimpleInstans(apikey)
                .JustTelempateSendSmsBatch(mobile, tpl_id, tpl_value, "", "", "");
            //问题处理
            return new SendResponse() { Code = Status.Success, Msg = "发送：" + response.total_count + "条" + "," + JsonConvert.SerializeObject(response.data) };
        }

        public SendResponse SendYunXinShiSimpleSms(string uid, string pwd, string mobile, string template, string content = "",
            string time = "", string mobileids = "")
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(template))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }

            ARC.YunXinShi.SMSService.Models.BaseResponse response = GetYunXinShiInstans(uid, pwd).SendSimple(mobile, content, template, time, mobileids);
            if (response.stat != "100")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.message };
            }
            return new SendResponse() { Code = Status.Success, Msg = response.message };
        }

        public SendResponse SendSearchYunXinShiSimpleSms(string uid, string pwd, string mobile, string content, string time = "",
            string mobileids = "")
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(mobile) || string.IsNullOrEmpty(content))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            ARC.YunXinShi.SMSService.Models.BaseResponse response = GetYunXinShiInstans(uid, pwd).SendSimple(mobile, content, time, time);
            if (response.stat != "100")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.message };
            }
            return new SendResponse() { Code = Status.Success, Msg = response.message };
        }
        #endregion
        #region 查询短信发送状态
        public AliYunSendDetailsResponse QuerySendDetails(string accessKeyId, string secret, string phoneNumber,
            string sendDate, int pageSize,
            int currentPage)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(sendDate))
            {
                return new AliYunSendDetailsResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            QuerySendSmsDetails response = GetAliYunSimpleInstans(accessKeyId, secret)
                .QueryDigitalSendDetails(phoneNumber, sendDate, pageSize, currentPage);
            if (response.Code != "OK")
            {
                return new AliYunSendDetailsResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AliYunSendDetailsResponse()
            {
                Code = Status.Success,
                Msg = response.Message,
                RequestId = response.RequestId,
                SmsSendDetailDTOs = JsonConvert.DeserializeObject<ARC.API.ExternalInterface.Models.SendDetails.SmsSendDetailDtoList>(JsonConvert.SerializeObject(response.SmsSendDetailDTOs)),
                TotalCount = response.TotalCount
            };
        }
        public QuerySimpleSmsSendedResponse QuerySendDetails(string apikey, string start_time, string end_time,
            string mobile = "",
            int page_num = 1, int page_size = 20, string type = "")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(start_time) || string.IsNullOrEmpty(end_time))
            {
                return new QuerySimpleSmsSendedResponse()
                {
                    error_msg = "必填参数不可为空",
                };
            }
            QuerySimpleSmsSendedResponse response = GetYunPianSimpleInstans(apikey).QuerySimpleSms(start_time, end_time, mobile, page_num, page_size, type);
            return response;
        }

        public SendedSimpleStatusResponse QuerySendDetails(string uid, string pwd)
        {
            return GetYunXinShiInstans(uid, pwd).SendedSimpleStatus();
        }
        #endregion
        #region 创建签名
        public AddSignResponse AddSimpleSign(string accessKeyId, string secret, string signName, string signSource, string remark = "", Dictionary<string, string> signFileList = null)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(signName) || string.IsNullOrEmpty(signSource))
            {
                return new AddSignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsSingResponse response = GetAliYunSimpleInstans(accessKeyId, secret).AddSimpleSmsSign(signName, signSource, remark, signFileList);
            if (response.Code != "OK")
            {
                return new AddSignResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddSignResponse() { Code = Status.Success, Msg ="添加成功!", RequestId = response.RequestId, SignName = response.SignName };
        }

        public AddSignResponse AddSimpleSign(string apikey, string sign, long prove_type = 0, string license_url = "", string license_base64 = "",
            bool notify = true, bool apply_vip = false,
            bool is_only_global = false, string industry_type = "其它")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(sign))
            {
                return new AddSignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddSimpleSmsSinSuccessResponse response = GetYunPianSimpleInstans(apikey).AddSimpleSinSms(sign, prove_type, license_url, license_base64, notify,
                apply_vip, is_only_global, industry_type);
            if (response.code != 0)
            {
                return new AddSignResponse() { Code = Status.Error, Msg = response.detail, SignName = sign };
            }
            return new AddSignResponse() { Code = Status.Success, Msg = "添加成功!", SignName = response.sign == null ? sign : "" }; ;
        }

        #endregion
        #region 查询签名状态
        public QuerySignResponse QuerySimpleSign(string accessKeyId, string secret, string signName)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(signName))
            {
                return new QuerySignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            QuerySimpleSmsSinResponse response = GetAliYunSimpleInstans(accessKeyId, secret).QuerySimpleSmsSign(signName);
            if (response.Code != "OK")
            {
                return new QuerySignResponse() { Code = Status.Error, Msg = response.Reason};
            }
            return new QuerySignResponse() { Code = Status.Success, CreateDate = response.CreateDate, Msg = response.Message, Reason = response.Reason, SignName = response.SignName, SignStatus = response.SignStatus };
        }

        public List<QuerySignResponse> QuerySimpleSign(string apikey, string sign, int page_num, int page_size)
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(sign))
            {
                return new List<QuerySignResponse>() { new QuerySignResponse() { Code = Status.ParamenterIsNull, Msg = "参数不能为空" } };
            }

            ARC.Yunpian.SMSService.Models.SimpleSmsModels.QuerySimpleSmsSinResponse response = GetYunPianSimpleInstans(apikey).QuerySimpleSinSms(sign, page_num, page_size);
            if (response.code != 0)
            {
                return new List<QuerySignResponse>() { new QuerySignResponse() { Code = Status.Error, Msg = response.msg } };
            }
            else
            {
                var List = new List<QuerySignResponse>();
                foreach (var item in response.sign)
                {

                    QuerySignResponse responses = new QuerySignResponse();
                    responses.SignName = item.sign;
                    responses.SignStatus = item.check_status == "CHECKING" ? 0 :
                        item.check_status == "FAIL" ? 2 :
                        item.check_status == "SUCCESS" ? 1 : 3;
                    if (responses.SignStatus == 0)
                    {
                        responses.Code = Status.Success;
                        responses.Msg = "正在审核";
                    }
                    else if (responses.SignStatus == 2)
                    {
                        responses.Code = Status.Success;
                        responses.Msg = item.remark;
                    }
                    else
                    {
                        responses.Code = Status.Success;
                        responses.Msg = item.remark;
                    }
                    List.Add(responses);
                }
                return List;
            }
        }
        #endregion
        #region 删除签名
        public AddSignResponse DeleteSimpleSmsSign(string accessKeyId, string secret, string signName)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(signName))
            {
                return new AddSignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsSingResponse response = GetAliYunSimpleInstans(accessKeyId, secret).DeleteSimpleSmsSign(signName);
            if (response.Code != "OK")
            {
                return new AddSignResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddSignResponse() { Code = Status.Success, Msg = "删除成功!", RequestId = response.RequestId, SignName = response.SignName };
        }

        #endregion
        #region 修改签名

        public AddSignResponse UpdateSimpleSign(string accessKeyId, string secret, string signName, string signSource,
            string remark,
            Dictionary<string, string> signFileList)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(signName) || string.IsNullOrEmpty(signSource) || string.IsNullOrEmpty(remark))
            {
                return new AddSignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsSingResponse response = GetAliYunSimpleInstans(accessKeyId, secret).ModifySimpleSmsSign(signName, signSource, remark, signFileList);
            if (response.Code != "OK")
            {
                return new AddSignResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddSignResponse() { Code = Status.Success, Msg = "修改成功!", RequestId = response.RequestId, SignName = response.SignName };
        }

        public AddSignResponse UpdateSimpleSign(string apikey, string old_sign, string sign, long prove_type,
            string license_url,
            string license_base64, bool notify = true, bool apply_vip = false,
            bool is_only_global = false, string industry_type = "其它")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(sign) || string.IsNullOrEmpty(old_sign))
            {
                return new AddSignResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddSimpleSmsSinSuccessResponse response = GetYunPianSimpleInstans(apikey).ModifySimpleSinSms(old_sign, sign, prove_type, license_url, license_base64, notify,
                apply_vip, is_only_global, industry_type);
            if (response.code != 0)
            {
                return new AddSignResponse() { Code = Status.Error, Msg = response.detail, SignName = sign };
            }
            return new AddSignResponse() { Code = Status.Success, Msg = "修改成功!", SignName = response.sign == null ? sign : "" }; ;
        }

        #endregion
        #region 创建模板
        public AddTemplateResponse AddSimpleTemplate(string accessKeyId, string secret, int templateType,
            string templateName,
            string templateContent, string remark = "")
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateName) || string.IsNullOrEmpty(templateContent) || templateType > 3)
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsTemplateResponse response = GetAliYunSimpleInstans(accessKeyId, secret)
                .AddSimpleSmsTemplate(templateType, templateName, templateContent, remark);
            if (response.Code != "OK")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "添加成功!", RequestId = response.RequestId, TemplateCode = response.TemplateCode };
        }

        public AddTemplateResponse AddSimpleTemplate(string apikey, string tpl_content, int notify_type = 0,
            string website = "", int tplType = 0, string callback = "", string apply_description = "")
        {
            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(tpl_content))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDeleteSimpleSmsTelResponse response = GetYunPianSimpleInstans(apikey).AddSimpleTelelempleSms(tpl_content, notify_type, website, tplType, callback,
                apply_description);
            if (response.code != 0)
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.detail };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "添加成功!", TemplateCode = response.tpl_id.ToString() };
        }

        public AddTemplateResponse AddSimpleTemplate(string uid, string pwd, string content, string type,
            string title = "", string dataformat = "")
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(content) || string.IsNullOrEmpty(type))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDeleAndUpdateSimpleTelResponse response = GetYunXinShiInstans(uid, pwd).AddSimpleTemplate(content, type, title, dataformat);
            if (response.stat != "100")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "添加成功!", TemplateCode = response.templateid.ToString() };
        }
        #endregion
        #region 查询模板状态
        public QueryTemplate QuerySimpleTemplate(string accessKeyId, string secret, string templateCode)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateCode))
            {
                return new QueryTemplate()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            QuerySimpleSmsTemplateResponse response = GetAliYunSimpleInstans(accessKeyId, secret).QuerySimpleSmsTemplate(templateCode);
            if (response.Code != "OK")
            {
                return new QueryTemplate() { Code = Status.Error, Msg = response.Message };
            }
            return new QueryTemplate() { Code = Status.Success, CreateDate = response.CreateDate, Msg = response.Message, Reason = response.Reason, TemplateCode = response.TemplateCode, TemplateContent = response.TemplateContent, TemplateName = response.TemplateName, TemplateStatus = response.TemplateStatus, TemplateType = response.TemplateType };
        }

        public QueryTemplate QuerySimpleTemplate(string apikey, long tpl_id)
        {
            if (string.IsNullOrEmpty(apikey))
            {
                return new QueryTemplate()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            QuerySimpleSmsTelTplIdResponse response = GetYunPianSimpleInstans(apikey).QuerySimpleTeleplateSms(tpl_id);
            if (response.code != 0)
            {
                return new QueryTemplate() { Code = Status.Error, Msg = response.msg };
            }
            return new QueryTemplate()
            {
                Code = Status.Success,
                TemplateCode = response.tpl_id.ToString(),
                Msg = response.reason,
                TemplateContent = response.tpl_content,
                TemplateStatus = response.check_status == "CHECKING" ? 0 : response.check_status == "SUCCESS" ? 1 : 2,

            };
        }
        public QueryTemplate QuerySimpleTemplate(string uid, string pwd, string templateid, string nul)
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(templateid))
            {
                return new QueryTemplate()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }

            QuerySimpleTelResponse response = GetYunXinShiInstans(uid, pwd).QuerySimpleTemplate(templateid);
            return new QueryTemplate() { Code = Status.Success, Msg = response.message, TemplateStatus = response.stat == "100" ? 1 : response.stat == "168" ? 2 : 0, Reason = response.question };
        }
        #endregion
        #region 删除模板
        public AddTemplateResponse DeleteSimpleTemplate(string accessKeyId, string secret,
            string templateName)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateName))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsTemplateResponse response = GetAliYunSimpleInstans(accessKeyId, secret).DeleteSimpleSmsTemplate(templateName);
            if (response.Code != "OK")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "删除成功!", RequestId = response.RequestId, TemplateCode = response.TemplateCode };
        }

        public AddTemplateResponse DeleteSimpleTemplate(string apikey, long tpl_id)
        {
            if (string.IsNullOrEmpty(apikey))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDeleteSimpleSmsTelResponse response = GetYunPianSimpleInstans(apikey).DeleteSimpleTelemplateSms(tpl_id);
            if (response.code != 0)
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.msg };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "删除成功!", TemplateCode = response.tpl_id.ToString() };
        }

        /// <summary>
        /// 删除普通短信模板
        /// </summary>
        /// <param name="content">模板内容包含短信内容和签名。全文变量模板的变量用{**}代替，JSON变量模板的变量用源码中相应的变量参数代替。</param>
        /// <param name="type">1.验证码 2.通知 3.推广</param>
        /// <param name="delete">delete=1 删除指定模板ID模板</param>
        /// <returns></returns>
        public AddTemplateResponse DeleteSimpleTemplate(string uid, string pwd, string content, string type,string templateId, string delete)
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(content) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(delete))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }

            AddAndDeleAndUpdateSimpleTelResponse response = GetYunXinShiInstans(uid, pwd).DeleteSimpleTemplate(content, type, templateId, delete);
            if (response.stat != "100")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "删除成功!", TemplateCode = response.templateid.ToString() };
        }
        #endregion

        #region 修改模板

        public AddTemplateResponse UpdateSimpleTemplate(string accessKeyId, string secret, int templateType,
            string templateName,
            string templateCode, string templateContent, string remark)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(templateCode) ||
                string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateName) ||
                string.IsNullOrEmpty(templateContent) || templateType > 3)
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDelSimpleSmsTemplateResponse response = GetAliYunSimpleInstans(accessKeyId, secret)
                .ModifySimpleSmsTemplate(templateType, templateName, templateCode, templateContent, remark);
            if (response.Code != "OK")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "修改成功!", RequestId = response.RequestId, TemplateCode = response.TemplateCode };
        }

        public AddTemplateResponse UpdateSimpleTemplate(string apikey, long tpl_id, string tpl_content,
            int notify_type = 0,
            string website = "", int tplType = 0, string callback = "", string apply_description = "")
        {

            if (string.IsNullOrEmpty(apikey) || string.IsNullOrEmpty(tpl_content))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDeleteSimpleSmsTelResponse response = GetYunPianSimpleInstans(apikey).ModifySimpleTelelempleSms(tpl_id, tpl_content, notify_type, website, tplType, callback,
                apply_description);
            if (response.code != 0)
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.detail };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "修改成功!", TemplateCode = response.tpl_id.ToString() };
        }

        public AddTemplateResponse UpdateSimpleTemplate(string uid, string pwd, string content, string type,
            string templateid)
        {
            if (string.IsNullOrEmpty(uid) || string.IsNullOrEmpty(pwd) || string.IsNullOrEmpty(content) || string.IsNullOrEmpty(type) || string.IsNullOrEmpty(templateid))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            AddAndDeleAndUpdateSimpleTelResponse response = GetYunXinShiInstans(uid, pwd).UpdateSimpleTemplate(content, type, templateid);
            if (response.stat != "100")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = "修改成功!", TemplateCode = response.templateid.ToString() };
        }
        #endregion
        #region 数字短信
        public AddTemplateResponse AddAliYunDigitalTemplate(string accessKeyId, string secret, string templateName,
          List<DigitalSmsAddTemFileArray> files, string remark)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateName))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            if (files == null) return new AddTemplateResponse() { Code = Status.ParamenterIsNull, Msg = "必填参数不可为空" };
            if (files.Count == 0) return new AddTemplateResponse() { Code = Status.ParamenterIsNull, Msg = "必填参数不可为0" };
            AddAndDelDigitalTemResponse response = GetAliYunDigitalInstans(accessKeyId, secret).AddDigitalTemplate(templateName, files, remark);
            if (response.Code != "OK")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = response.Message, RequestId = response.RequestId, TemplateCode = response.TemplateCode };
        }

        public AddTemplateResponse DeleALiYunDigitalTemplate(string accessKeyId, string secret, string templateCode)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateCode))
            {
                return new AddTemplateResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }

            AddAndDelDigitalTemResponse response = GetAliYunDigitalInstans(accessKeyId, secret).DeleDigitalTemplate(templateCode);
            if (response.Code != "Ok")
            {
                return new AddTemplateResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AddTemplateResponse() { Code = Status.Success, Msg = response.Message, RequestId = response.RequestId, TemplateCode = response.TemplateCode };
        }
        public AliYunSendDetailsResponse QueryAliYunDigitalSendDetails(string accessKeyId, string secret,
            string phoneNumber, string sendDate, int pageSize,
            int currentPage)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(phoneNumber) || string.IsNullOrEmpty(sendDate))
            {
                return new AliYunSendDetailsResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                };
            }
            QuerySendSmsDetails response = GetAliYunSimpleInstans(accessKeyId, secret)
                .QueryDigitalSendDetails(phoneNumber, sendDate, pageSize, currentPage);
            if (response.Code != "OK")
            {
                return new AliYunSendDetailsResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new AliYunSendDetailsResponse()
            {
                Code = Status.Success,
                Msg = response.Message,
                RequestId = response.RequestId,
                SmsSendDetailDTOs = JsonConvert.DeserializeObject<ARC.API.ExternalInterface.Models.SendDetails.SmsSendDetailDtoList>(JsonConvert.SerializeObject(response.SmsSendDetailDTOs)),
                TotalCount = response.TotalCount
            };
        }
        public SendResponse SendALiYunDigitalSms(string accessKeyId, string secret, string phoneNumbers,
         string templateCode, Dictionary<string, string> templateParam = null)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(phoneNumbers) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateCode))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            ARC.AliYun.SMSService.Models.SendSmsResponse response = GetAliYunDigitalInstans(accessKeyId, secret)
                .SendDigitalSms(phoneNumbers, templateCode, templateParam);
            if (response.Code != "OK")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new SendResponse() { Code = Status.Success, BilzId = response.BizId, Msg = response.Message };
        }

        public SendResponse SendALiYunDigitalBatch(string accessKeyId, string secret, List<string> phoneNumbers, string templateCode,
            List<Dictionary<string, string>> templateParam = null)
        {
            if (string.IsNullOrEmpty(accessKeyId) || string.IsNullOrEmpty(secret) || string.IsNullOrEmpty(templateCode))
            {
                return new SendResponse()
                {
                    Code = Status.ParamenterIsNull,
                    Msg = "必填参数不可为空",
                    BilzId = "0"
                };
            }
            if (phoneNumbers == null) return new SendResponse() { Code = Status.ParamenterIsNull, Msg = "电话号码不能为null" };
            if (phoneNumbers.Count == 0) return new SendResponse() { Code = Status.ParamenterIsNull, Msg = "电话号码不能为0" };
            ARC.AliYun.SMSService.Models.SendSmsResponse response = GetAliYunDigitalInstans(accessKeyId, secret).SendDigitalSmsBatch(phoneNumbers, templateCode, templateParam);
            if (response.Code != "OK")
            {
                return new SendResponse() { Code = Status.Error, Msg = response.Message };
            }
            return new SendResponse() { Code = Status.Success, BilzId = response.BizId, Msg = response.Message };
        }
        #endregion
        /// <summary>
        /// 阿里云普通短信实例
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        private ARC.AliYun.SMSService.Interfaces.ISimpleSms GetAliYunSimpleInstans(string accessKeyId, string secret)
        {
            return new ARC.AliYun.SMSService.Service.SimpleSmsService.SimpleSmsService(accessKeyId, secret);
        }
        /// <summary>
        /// 阿里云数字短信实例
        /// </summary>
        /// <param name="accessKeyId"></param>
        /// <param name="secret"></param>
        /// <returns></returns>
        private ARC.AliYun.SMSService.Interfaces.IDigitalSms GetAliYunDigitalInstans(string accessKeyId, string secret)
        {
            return new ARC.AliYun.SMSService.Service.DigitalSmsService.DigitalSmsService(accessKeyId, secret);
        }
        /// <summary>
        /// 云片普通短信实例
        /// </summary>
        /// <param name="apikey"></param>
        /// <returns></returns>
        private ARC.Yunpian.SMSService.Interfaces.ISimpleSms GetYunPianSimpleInstans(string apikey)
        {
            return new ARC.Yunpian.SMSService.Service.SimpleSmsService.SimpleSms(apikey);
        }
        /// <summary>
        /// 云信使
        /// </summary>
        /// <param name="uid"></param>
        /// <param name="pwd"></param>
        /// <returns></returns>
        private ARC.YunXinShi.SMSService.Interfaces.ISimpleSms GetYunXinShiInstans(string uid, string pwd)
        {
            return new ARC.YunXinShi.SMSService.Service.SimpleSmsService.SimpleSms(uid, pwd);
        }
    }
}
